//
//  PictureSelector.m
//  baseApp
//
//  Created by WangRen on 2019/3/28.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "PictureSelector.h"
#import "TZImagePickerController.h"
#import <React/RCTUtils.h>
@implementation PictureSelector
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(open:(RCTResponseSenderBlock)callback)
{
  TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
 // imagePickerVc.maxImagesCount = self.maxCountTF.text.integerValue;
  imagePickerVc.allowPickingGif = false;
  imagePickerVc.allowPickingOriginalPhoto = true;
  imagePickerVc.allowCrop = true;
  imagePickerVc.needCircleCrop =false;
  imagePickerVc.isCropSetSize=true;
  imagePickerVc.cropImageSize=CGRectMake(0,0,100,100);
//  int CropW=100;
//  int CropH=100;
//  CGFloat x = ([[UIScreen mainScreen] bounds].size.width - CropW) / 2;
//  CGFloat y = ([[UIScreen mainScreen] bounds].size.height - CropH) / 2;
//
//  NSInteger left = 30;
//  NSInteger x = [[UIScreen mainScreen] bounds].size.width - 2 * left;
//  NSInteger y = [[UIScreen mainScreen] bounds].size.height / 2;
//  imagePickerVc.cropRect = CGRectMake(left, top, widthHeight, widthHeight);
// //  NSLog(@"x=%f,y=%f",x,y);
//  imagePickerVc.cropRect = CGRectMake(x,y,CropW,CropH);
  // 设置竖屏下的裁剪尺寸
  CGFloat margin = 40.0f;
  CGFloat width = [UIScreen mainScreen].bounds.size.width - 2 * margin;
  CGFloat height = width;
  CGFloat y = ([UIScreen mainScreen].bounds.size.height - height) / 2;
  imagePickerVc.cropRect = CGRectMake(margin, y, width, height);
   NSLog(@"x=%f----->",width);
  [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
   
     NSMutableArray *selectedPhotos = [NSMutableArray array];
    for(UIImage* imgItem in photos){
      [selectedPhotos addObject:[self handleImageData:imgItem quality:70]];//self.handleImageData(imgItem,70);
    }
     callback(@[[NSNull null], selectedPhotos]);
    NSLog(@"----->%@",selectedPhotos);
  }];
  [[self topViewController] presentViewController:imagePickerVc animated:YES completion:nil];
}


  
- (NSDictionary *)handleImageData:(UIImage *) image quality:(NSInteger)quality {
  NSMutableDictionary *photo = [NSMutableDictionary dictionary];
  NSData *imageData = UIImageJPEGRepresentation(image, quality * 1.0 / 100);
  
  // 剪切图片并放在tmp中
  photo[@"width"] = @(image.size.width);
  photo[@"height"] = @(image.size.height);
  photo[@"size"] = @(imageData.length);
  
  NSString *fileName = [NSString stringWithFormat:@"%@.jpg", [[NSUUID UUID] UUIDString]];
  [self createDir];
  NSString *filePath = [NSString stringWithFormat:@"%@ImageCaches/%@", NSTemporaryDirectory(), fileName];
  if ([imageData writeToFile:filePath atomically:YES]) {
    photo[@"uri"] = filePath;
  } else {
    NSLog(@"保存压缩图片失败%@", filePath);
  }
  
  
  return photo;
}

- (BOOL)createDir {
  NSString * path = [NSString stringWithFormat:@"%@ImageCaches", NSTemporaryDirectory()];;
  NSFileManager *fileManager = [NSFileManager defaultManager];
  BOOL isDir;
  if  (![fileManager fileExistsAtPath:path isDirectory:&isDir]) {//先判断目录是否存在，不存在才创建
    BOOL res=[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    return res;
  } else return NO;
}

- (UIViewController *)topViewController {
  UIViewController *rootViewController = RCTPresentedViewController();
  return rootViewController;
}
@end
